package icu.makka.yygh.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.makka.yygh.enums.OrderStatusEnum;
import icu.makka.yygh.model.order.OrderInfo;
import icu.makka.yygh.order.service.OrderInfoService;
import icu.makka.yygh.order.service.impl.OrderInfoServiceImpl;
import icu.makka.yygh.result.Result;
import icu.makka.yygh.result.helper.AuthContextHolder;
import icu.makka.yygh.vo.order.OrderQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Autowired
    private OrderInfoServiceImpl orderInfoService;

    /**
     * 创建订单
     * @param scheduleId
     * @param patientId
     * @return
     */
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(@PathVariable String scheduleId, @PathVariable Long patientId) {
        log.info("创建订单，scheduleId={}，patientId={}", scheduleId, patientId);
        return Result.ok(orderInfoService.saveOrder(scheduleId, patientId));
    }

    /**
     * 订单列表
     * @param page
     * @param limit
     * @param orderQueryVo
     * @param request
     * @return
     */
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit,
                       OrderQueryVo orderQueryVo, HttpServletRequest request) {
        log.info("订单列表，page={}，limit={}，orderQueryVo={}", page, limit, orderQueryVo);
        //设置用户id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderInfoService.selectPage(pageParam, orderQueryVo);
        //返回结果
        return Result.ok(pageModel);
    }

    /**
     * 获取订单状态
     * @return
     */
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        log.info("获取订单状态");
        return Result.ok((OrderStatusEnum.getStatusList()));
    }

    /**
     * 根据订单id查询
     * @param orderId
     * @return
     */
    @GetMapping("auth/getOrderInfo/{orderId}")
    public Result getOrders(@PathVariable String orderId) {
        log.info("根据订单id查询订单详情，orderId={}", orderId);
        OrderInfo orderInfo = orderInfoService.getOrder(orderId);
        return Result.ok(orderInfo);
    }

    /**
     * 取消预约
     * @param orderId
     * @return
     */
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable("orderId") Long orderId) {
        log.info("取消预约，orderId={}", orderId);
        boolean flag = orderInfoService.cancelOrder(orderId);
        log.info("调用业务层的返回结果为{}", flag);
        return flag ? Result.ok() : Result.fail();
    }
}
